package com.jerry.android.TabBar.tabhost;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.jerry.android.R;
import com.jerry.android.TabBar.fragment.AddressFragment;
import com.jerry.android.TabBar.fragment.SettingFragment;
import com.jerry.android.TabBar.fragment.WeixinFragment;

public class TabHostActivity extends FragmentActivity {

	private static final String TAB_TAG_WEIXIN = "weixin";
	private static final String TAB_TAG_ADDRESS = "address";
	private static final String TAB_TAG_SETTING= "setting";
	
	private TabHost tabHost;
	private FragmentTransaction fragmentTransaction;
	private WeixinFragment weixinFragment;
	private AddressFragment addressFragment;
	private SettingFragment settingFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_tabhost);
		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();
		
		initTabHost();
	}

	OnTabChangeListener tabChangeListener = new OnTabChangeListener() {
		@Override
		public void onTabChanged(String tabTag) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			weixinFragment = (WeixinFragment) fragmentManager.findFragmentByTag(TAB_TAG_WEIXIN);
			addressFragment = (AddressFragment) fragmentManager.findFragmentByTag(TAB_TAG_ADDRESS);
			settingFragment = (SettingFragment) fragmentManager.findFragmentByTag(TAB_TAG_SETTING);
			
			fragmentTransaction = fragmentManager.beginTransaction();
			detachFragment();
			showTabFragment(tabTag);
			fragmentTransaction.commit();	
		}
	};
	
	private void detachFragment() {
		if (weixinFragment != null) {
			fragmentTransaction.detach(weixinFragment);
		}
		if (addressFragment != null) {
			fragmentTransaction.detach(addressFragment);
		}
		if (settingFragment != null) {
			fragmentTransaction.detach(settingFragment);
		}
	}
	
	private void showTabFragment(String tabTag) {
		int tabId = getTabId(tabTag);
		switch (tabId) {
		case 1:
			showWeiXinFragment();
			break;
		case 2:
			showAddressFragment();
			break;
		case 3:
			showSettingFragment();
			break;
		default :
			showWeiXinFragment();
			break;
		}
	}
	
	private int getTabId(String tabTag) {
		int tabId = 0;
		if(tabTag.equalsIgnoreCase(TAB_TAG_WEIXIN)){
			tabId = 1;
		} else if(tabTag.equalsIgnoreCase(TAB_TAG_ADDRESS)){
			tabId = 2;
		} else if(tabTag.equalsIgnoreCase(TAB_TAG_SETTING)){
			tabId = 3;
		} 
		return tabId;
	}
	
	public void showWeiXinFragment() {
		if (weixinFragment == null) {
			fragmentTransaction.add(android.R.id.tabcontent, new WeixinFragment(), TAB_TAG_WEIXIN);
		} else {
			fragmentTransaction.attach(weixinFragment);
		}
	}
	
	public void showAddressFragment() {
		if (addressFragment == null) {
			fragmentTransaction.add(android.R.id.tabcontent, new AddressFragment(), TAB_TAG_ADDRESS);
		} else {
			fragmentTransaction.attach(addressFragment);
		}
	}
	
	public void showSettingFragment() {
		if (settingFragment == null) {
			fragmentTransaction.add(android.R.id.tabcontent, new SettingFragment(), TAB_TAG_SETTING);
		} else {
			fragmentTransaction.attach(settingFragment);
		}
	}
	
	private void initTabHost() {

		tabHost.addTab(tabHost
				.newTabSpec(TAB_TAG_WEIXIN)
				.setIndicator(new TabIndicator(this, TAB_TAG_WEIXIN))
				.setContent(new DefaultTabContentFragment(getBaseContext())));
		
		tabHost.addTab(tabHost
				.newTabSpec(TAB_TAG_ADDRESS)
				.setIndicator(new TabIndicator(this, TAB_TAG_ADDRESS))
				.setContent(new DefaultTabContentFragment(getBaseContext())));

		tabHost.addTab(tabHost
				.newTabSpec(TAB_TAG_SETTING)
				.setIndicator(new TabIndicator(this, TAB_TAG_SETTING))
				.setContent(new DefaultTabContentFragment(getBaseContext())));

        tabHost.setCurrentTab(1);
        tabHost.setOnTabChangedListener(tabChangeListener);
        tabHost.setCurrentTab(0);
	}

	public class TabIndicator extends LinearLayout {
		public TabIndicator(Context context, String title) {
			super(context);
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View tab = inflater.inflate(
					R.layout.activity_tab_indicator, this);
			if (title != null && title.length() > 0) {
				TextView txtView = (TextView) tab.findViewById(R.id.tab_title);
				txtView.setText(title);
			}
		}
	}

}
